function changeTime(date) {
    var dateee = new Date(date).toJSON();
    return new Date(new Date(dateee) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[0\d]{3}Z/, '');
}
// 创造页面的函数，四个参数分别为：当前页面，发送ajax请求的地址，删除按钮处的字符串（为空时无删除选项），用户id
function createPage(currentPage, url, a_string, id) {
    var dynamicWrapper = document.getElementById("dynamic-wrapper");
    var load = document.getElementById("load");
    var changePage = document.getElementsByClassName("changePage");
    var current = document.getElementById("current");
    var pages = document.getElementById("pages");
    load.style.display = "none";
    var currentNum = current.innerHTML;
    // 当id不存在时，该函数用于创建全部动态的页面，存在时，可以设置某个人的动态页面
    if (!id) {
        var data = {
            current: currentPage,
            size: 4
        }
    } else {
        var data = {
            userId: id,
            current: currentPage,
            size: 4,
        }
    }
    // 查找动态的请求
    ajax({
        type: "get",
        url: url,
        data: data,
        success: function (response) {
            dynamicWrapper.innerHTML = "";
            current.innerHTML = response.data.current;
            pages.innerHTML = response.data.pages;
            if (response.data.records) {
                var obj = response.data.records;
            } else {
                obj = response;
            }
            // 动态创建  每个动态节点
            for (let i = 0; i < obj.length; i++) {
                var F_div = document.createElement("div");
                var h2 = document.createElement("h2");
                var span = document.createElement("span");
                var a = document.createElement("a");
                var h3 = document.createElement("h3");
                var S_div = document.createElement("div");
                var p = document.createElement("p");
                span.innerHTML = obj[i].id;
                var time = changeTime(obj[i].creationTime);
                h2.innerHTML = '&nbsp' + '&nbsp' + '&nbsp' + '&nbsp' + '&nbsp' + obj[i].username + '&nbsp' + '&nbsp' + '&nbsp' + '&nbsp' + '&nbsp' + time;
                h3.innerHTML = obj[i].title;
                p.innerHTML = obj[i].content;
                S_div.appendChild(span);
                S_div.appendChild(h2);
                S_div.appendChild(h3);
                S_div.appendChild(p);
                // 如果动态有图片的话，则创建节点存放图片
                if (obj[i].hasPicture == 1 && obj[i].urls.length != 0) {
                    var img = document.createElement("img");
                    img.src = "http://www.rushmc.top/" + obj[i].urls[0];
                    S_div.appendChild(img);
                }
                // 如果a的字符串参数存在，则a存在，否则不插入每个节点，即不提供删除功能
                if (a_string == "") {
                    a.style.display = "none";
                } else {
                    a.innerHTML = a_string;
                    F_div.appendChild(a);
                }
                F_div.appendChild(S_div);
                // 为每个 a 设置点击函数，完成删除动态的功能
                a.onclick = function (event) {
                    // 获取要删除的动态ID
                    var dynamicId = event.target.nextElementSibling.firstElementChild.innerHTML;
                    ajax({
                        type: "post",
                        url: "http://www.rushmc.top/api/dynamicState/delete",
                        data: {
                            id: dynamicId
                        },
                        success: function (response) {
                            event.target.style.color = "red";
                            event.target.innerHTML = "删除成功！";
                            createPage(currentNum, "http://www.rushmc.top/api/dynamicState/select", "删除动态", id)
                        }
                    })
                }
                // 为每个节点的动态内容设置点击函数，用于跳转页面查看动态详情
                S_div.onclick = function (event) {
                    event.cancelBubble = 'true';
                    if (event.target.innerHTML) {
                        var dynamicid = event.target.firstChild.innerHTML;
                    }
                    if (!dynamicid) {
                        dynamicid = event.target.parentNode.firstChild.innerHTML;
                    }
                    setSession("dynamicid", dynamicid);
                    document.location = "../" + "动态详情/index5.html";
                }
                dynamicWrapper.appendChild(F_div);
            }
            // 为页面跳转按钮设置点击函数，实现上一页下一页的跳转
            // 当页面不为第一张时，有上一页的功能
            if (parseInt(current.innerHTML) != 1) {
                changePage[0].onclick = function () {
                    currentNum = parseInt(current.innerHTML) - 1;
                    if (!id) {
                        createPage(currentNum, "http://www.rushmc.top/api/dynamicState/select/page", "")
                    } else {
                        createPage(currentNum, "http://www.rushmc.top/api/dynamicState/select", "删除动态", id)
                    }
                }
            }
            // 当页面不为最后一张时，有查找下一页的功能
            if (current.innerHTML != pages.innerHTML) {
                changePage[1].onclick = function () {
                    currentNum = parseInt(current.innerHTML) + 1;
                    if (!id) {
                        createPage(currentNum, "http://www.rushmc.top/api/dynamicState/select/page", "")
                    } else {
                        createPage(currentNum, "http://www.rushmc.top/api/dynamicState/select", "删除动态", id)
                    }
                }
            }
            // 实现页码的跳转功能
            changePage[3].onclick = function () {
                var pagesNum = parseInt(pages.innerHTML);
                if (changePage[2].value && changePage[2].value <= pagesNum) {
                    currentNum = changePage[2].value;
                    if (!id) {
                        createPage(currentNum, "http://www.rushmc.top/api/dynamicState/select/page", "")
                    } else {
                        createPage(currentNum, "http://www.rushmc.top/api/dynamicState/select", "删除动态", id)
                    }
                }
            }
        }
    })
}